import React from 'react'
import PropTypes from 'prop-types'
import Component, { mapStateToProps, mapDispatchToProps } from '@components/Component'
import { connect } from 'react-redux'
import { message } from 'antd'
import Approve from '@components/ApprovePre/Index'
import StepZeroInput from './CloseCase/StepZeroInput'
import StepZeroHistory from './CloseCase/StepZeroHistory'
import SecretaryInput from './CloseCase/SecretaryInput'
import SecretaryHistory from './CloseCase/SecretaryHistory'
import StepTwoInput from './CloseCase/StepTwoInput'
import StepTwoHistory from './CloseCase/StepTwoHistory'
import { requestFiles } from '@js/request'
import StepThreeInput from './CloseCase/StepThreeInput'
import StepThreeHistory from './CloseCase/StepThreeHistory'
import StepFourInput from './CloseCase/StepFourInput'
import StepFourHistory from './CloseCase/StepFourHistory'
import { awaitWrap } from '@js/tool'

class CloseCase extends Component {
  state = {
    dictionaries: {}
  }

  // 获取枚举的字典值
  getDictionary () {
    this.$get('/api/xapc/dictionary', {
      types: '结案类型,仲裁庭核阅意见反馈,结案缓急'
    }).then(res => {
      if (res.status === 1) {
        // 设置枚举字典
        const dictionaries = this.formatDictionaries(res.data)
        this.setState({
          dictionaries
        })
      } else {
        message.error(res.message)
      }
    })
  }

  // 格式化枚举字典
  formatDictionaries (dictionaries = []) {
    const result = {}
    dictionaries.forEach(d => {
      result[d.type] = d.list
    })
    return result
  }

  async zeroStepSubmit (instance, data) {
    data.vars.closure_doc_datetime = Math.floor(data.vars.closure_doc_datetime.valueOf() / 1000)
    return data
  }

  async secretaryBeforeSubmit (instance, data) {
    const hide = message.loading('文书提交中', 120)
    let fileid = ''
    const file = data.file ? data.file.fileList : []
    if (file && file.length) {
      const [e, d] = await awaitWrap(requestFiles(file))
      if (e) {
        return message.warning(e.message)
      }
      fileid = d.map(li => li.id).join()
    }
    hide()
    data.vars.closure_document = fileid
    delete data.file
    return data
  }

  async threeStepSubmit (instance, data) {
    const hide = message.loading('提交中', 120)
    let fileid = ''
    const file = data.file ? data.file.fileList : []
    if (file && file.length) {
      const [e, d] = await awaitWrap(requestFiles(file))
      if (e) {
        hide()
        return message.warning(e.message)
      }
      fileid = d.map(li => li.id).join()
    }
    hide()
    data.vars.closure_document_fixed = fileid
    delete data.file
    return data
  }

  beforeSubmit (instance, data) {
    switch (instance.currentStep.step_key) {
      case 'jiean00':
        return this.zeroStepSubmit(instance, data)
      case 'jiean01':
        return this.secretaryBeforeSubmit(instance, data)
      case 'jiean03':
        return this.threeStepSubmit(instance, data)
      default:
        return data
    }
  }

  // 获取表单初始化数据
  getFormInitialData (instance) {
    switch (instance.currentStep.step_key) {
      case 'jiean03':
        return {
          vars: {
            closure_isonlineaudit: '否'
          }
        }
      default:
        return {}
    }
  }

  componentWillMount () {
    this.getDictionary()
  }

  // 渲染当前操作步骤线上部分
  renderCurrentStepLineTop (formRef, instance, renderFn) {
    switch (instance.currentStep.step_key) {
      case 'jiean00':
        return <StepZeroInput instance={instance} userInfo={this.props.userInfo} caseDetail={this.props.caseDetail} formRef={formRef} dictionaries={this.state.dictionaries} />
      case 'jiean01':
        return <SecretaryInput instance={instance} userInfo={this.props.userInfo} caseDetail={this.props.caseDetail} formRef={formRef} dictionaries={this.state.dictionaries} />
      case 'jiean02':
        return <StepTwoInput instance={instance} userInfo={this.props.userInfo} caseDetail={this.props.caseDetail} formRef={formRef} dictionaries={this.state.dictionaries} />
      case 'jiean03':
        return <StepThreeInput instance={instance} userInfo={this.props.userInfo} caseDetail={this.props.caseDetail} formRef={formRef} dictionaries={this.state.dictionaries} />
      case 'jiean04':
        return <StepFourInput instance={instance} userInfo={this.props.userInfo} caseDetail={this.props.caseDetail} formRef={formRef} dictionaries={this.state.dictionaries} />
      default:
        return renderFn()
    }
  }

  // 渲染历史
  renderHistory (action, renderFn, instance, renderDocument) {
    const key = action.step_key
    // const isDisagree = action.opt_add === 'disagree'
    switch (key) {
      case 'jiean00':
        return <StepZeroHistory key={action.id} userInfo={this.props.userInfo} instance={instance} action={action} renderDocument={renderDocument} />
      case 'jiean01':
        return <SecretaryHistory key={action.id} userInfo={this.props.userInfo} instance={instance} action={action} renderDocument={renderDocument} />
      case 'jiean02':
        return <StepTwoHistory key={action.id} userInfo={this.props.userInfo} instance={instance} action={action} renderDocument={renderDocument} />
      case 'jiean03':
        return <StepThreeHistory key={action.id} userInfo={this.props.userInfo} instance={instance} action={action} renderDocument={renderDocument} />
      case 'jiean04':
        return <StepFourHistory key={action.id} userInfo={this.props.userInfo} instance={instance} action={action} renderDocument={renderDocument} />
    }
    return renderFn(action)
  }

  render () {
    return (
      <div>
        <Approve
          caseDetail={this.props.caseDetail}
          flowKey='jiean'
          afterAction={(instance, action) => this.props.refreshCaseData()}
          creatable={this.props.editable}
          renderCurrentStepLineTop={(...params) => this.renderCurrentStepLineTop(...params)}
          beforeSubmit={(...params) => this.beforeSubmit(...params)}
          initialCurrentStepFormData={instance => this.getFormInitialData(instance)}
          renderHistory={(h, renderFn, instance, renderDocument) => this.renderHistory(h, renderFn, instance, renderDocument)}
        />
      </div>
    )
  }
}

CloseCase.propTypes = {
  caseDetail: PropTypes.object
  // active: PropTypes.bool,
  // refreshCaseData: PropTypes.func
  // editable: PropTypes.bool,
  // fileList: PropTypes.array
  // getFileList: PropTypes.func
}
CloseCase.defaultProps = {
  caseDetail: {},
  active: false,
  refreshCaseData: () => {},
  editable: false,
  fileList: [],
  getFileList: () => {}
}

const mapState = mapStateToProps(function (state) {
  return {
    userInfo: state.basic.userInfo
  }
})

const mapDispatch = mapDispatchToProps(function (dispatch) {
  return {

  }
})

export default connect(mapState, mapDispatch)(CloseCase)
